package com.xj.qq;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.xj.qq.fragments.ConversationFragment;
import com.xj.qq.fragments.ListFragment;
import com.xj.qq.fragments.PhoneFragment;
import com.xj.qq.sliding_menu.SlidingMenu;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView conversation;
    private ImageView contact;
    private ImageView plugin;
    private TextView message;
    private TextView phone;
    private TextView tv2;
    private SlidingMenu slidingMenu;
    private ImageView header_userIcon;
    private ImageView header_more;
    private int colorWhite;
    private int colorBlue;
    private FragmentManager fm;
    private ConversationFragment message_fragment;
    private PhoneFragment phone_fragment;
    private ListFragment list_fragment;
    private Button set;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getFragmentManager();
        initView();
        Setting();
    }

    private void initView(){
        colorWhite = getResources().getColor(R.color.colorWhite);
        colorBlue = getResources().getColor(R.color.colorBlue);

        conversation = (ImageView) findViewById(R.id.iv_conversation);
        contact = (ImageView) findViewById(R.id.iv_contact);
        plugin = (ImageView) findViewById(R.id.iv_plugin);
        conversation.setSelected(true);
        conversation.setOnClickListener(this);
        contact.setOnClickListener(this);
        plugin.setOnClickListener(this);

        message = (TextView) findViewById(R.id.tv_message);
        phone = (TextView) findViewById(R.id.tv_phone);
        message.setOnClickListener(this);
        phone.setOnClickListener(this);

        message.setTextColor(colorWhite);
        phone.setTextColor(colorBlue);
        message.setSelected(true);

        slidingMenu = (SlidingMenu) findViewById(R.id.sliding_menu);
        header_userIcon = (ImageView) findViewById(R.id.iv_user_icon);
        header_userIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggle();
            }
        });

        header_more = (ImageView) findViewById(R.id.iv_header_btn_more);
        final PopupMenu pop = new PopupMenu(this,header_more);
        getMenuInflater().inflate(R.menu.menu_main, pop.getMenu());
        header_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()){
                            case R.id.scanf:

                        }
                        return false;
                    }
                });
                pop.show();
            }
        });

        message_fragment = new ConversationFragment();
        phone_fragment = new PhoneFragment();
        list_fragment = new ListFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, message_fragment);
        ft.commit();

        Intent intent = this.getIntent();
        tv2 = (TextView) findViewById(R.id.view2);
        String str = intent.getStringExtra("username");
        tv2.setText(str);
        set = (Button) findViewById(R.id.view5);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_conversation:
                setImageViewSelectState(v);
                break;
            case R.id.iv_contact:
                setImageViewSelectState(v);
                break;
            case R.id.iv_plugin:
                setImageViewSelectState(v);
                break;
            case R.id.tv_message:
                if (!message.isSelected()){
                    FragmentTransaction ft1 = fm.beginTransaction();
                    ft1.replace(R.id.fragment_container,message_fragment);
                    ft1.commit();
                }

                setTextSelectState(v);
                break;
            case R.id.tv_phone:
                if (!phone.isSelected()){
                    FragmentTransaction ft2 = fm.beginTransaction();
                    ft2.replace(R.id.fragment_container,phone_fragment);
                    ft2.commit();
                }

                setTextSelectState(v);
                break;
        }
    }

    private void setImageViewSelectState(View v){
        switch (v.getId()){
            case R.id.iv_conversation:

                if (!conversation.isSelected()){
                    conversation.setSelected(true);
                    contact.setSelected(false);
                    plugin.setSelected(false);
                    FragmentTransaction ft3 = fm.beginTransaction();
                    ft3.replace(R.id.fragment_container,message_fragment);
                    ft3.commit();
                }
                break;
            case R.id.iv_contact:

                if (!contact.isSelected()){
                    contact.setSelected(true);
                    conversation.setSelected(false);
                    plugin.setSelected(false);
                    FragmentTransaction ft4 = fm.beginTransaction();
                    ft4.replace(R.id.fragment_container,list_fragment);
                    ft4.commit();
                }
                break;
            case R.id.iv_plugin:

                if (!plugin.isSelected()){
                    plugin.setSelected(true);
                    contact.setSelected(false);
                    conversation.setSelected(false);
                    Intent intent_load = new Intent(MainActivity.this, Loading.class);
                    startActivity(intent_load);
                }
                break;
        }
    }

    private void setTextSelectState(View view){
        switch (view.getId()){
            case R.id.tv_message:
                if (!message.isSelected()){
                    message.setSelected(true);
                    message.setTextColor(colorWhite);
                    phone.setSelected(false);
                    phone.setTextColor(colorBlue);
                }
                break;
            case R.id.tv_phone:
                if (!phone.isSelected()){
                    phone.setSelected(true);
                    phone.setTextColor(colorWhite);
                    message.setSelected(false);
                    message.setTextColor(colorBlue);
                }
                break;
        }
    }
    public void Setting(){
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Back.class);
                String str = tv2.getText().toString();
                intent.putExtra("user_N", str);
                startActivity(intent);
                finish();
            }
        });
    }
}
